/*
 * @message: 描述
 * @Author: Roy
 * @Email: @163.com
 * @Github: @163.com
 * @Date: 2021-11-20 14:25:57
 * @LastEditors: Roy
 * @LastEditTime: 2021-11-21 14:46:20
 * @Deprecated: 否
 * @FilePath: /train-ticket/src/pages/index/store/actionCreators.js
 */
import * as actionTypes from './actionTypes'
import { baseURL } from '../../../const'


export function setFrom(from) {
    return {
        type: actionTypes.SET_FROM,
        payload: from,
    };
}
export function setTo(to) {
    return {
        type: actionTypes.SET_TO,
        payload: to,
    };
}
export function setDepartDate(departDate) {
    return {
        type: actionTypes.SET_DEPART_DATE,
        payload: departDate,
    };
}
export function setHighSpeed(highSpeed) {
    return {
        type: actionTypes.SET_HIGH_SPEED,
        payload: highSpeed,
    };
}

export function setIsLoadingCityData(isLoadingCityData) {
    return {
        type: actionTypes.SET_IS_LOADING_CITY_DATA,
        payload: isLoadingCityData,
    };
}

export function setCityData(cityDate) {
    return {
        type: actionTypes.SET_CITY_DATA,
        payload: cityDate,
    };
}

export function toggleHighSpeed() {
    return (dispatch, getState) => {
        const state = getState();
        const highSpeed = state.getIn(['index', 'highSpeed'])
        dispatch(setHighSpeed(!highSpeed));
    };
}

//显示城市选择
export function showCitySelector(currentSelectingLeftCity) {
    return dispatch => {
        dispatch({
            type: actionTypes.SET_IS_CITY_SELECTOR_VISIBLE,
            payload: true,
        });

        dispatch({
            type: actionTypes.SET_CURRENT_SELECTING_LEFT_CITY,
            payload: currentSelectingLeftCity,
        });
    };
}
//隐藏城市选择
export function hideCitySelector() {
    return {
        type: actionTypes.SET_IS_CITY_SELECTOR_VISIBLE,
        payload: false,
    };
}
//选择城市
export function setSelectedCity(city) {
    return (dispatch, getState) => {
        const state = getState();
        const currentSelectingLeftCity = state.getIn(['index', 'currentSelectingLeftCity'])
        if (currentSelectingLeftCity) {
            dispatch(setFrom(city));
        } else {
            dispatch(setTo(city));
        }

        dispatch(hideCitySelector());
    };
}
//显示日期选择
export function showDateSelector() {
    return {
        type: actionTypes.SET_IS_DATE_SELECTOR_VISIBLE,
        payload: true,
    };
}
//隐藏日期选择
export function hideDateSelector() {
    return {
        type: actionTypes.SET_IS_DATE_SELECTOR_VISIBLE,
        payload: false,
    };
}
//切换起点终点
export function exchangeFromTo() {
    return (dispatch, getState) => {
        const state = getState();
        dispatch(setFrom(state.getIn(['index', 'to'])));
        dispatch(setTo(state.getIn(['index', 'from'])));
    };
}

export function fetchCityData() {
    return (dispatch, getState) => {
        const state = getState();
        const isLoadingCityData = state.getIn(['index', 'isLoadingCityData'])
        if (isLoadingCityData) {
            return;
        }

        const cache = JSON.parse(
            localStorage.getItem('city_data_cache') || '{}'
        );

        if (Date.now() < cache.expires) {
            dispatch(setCityData(cache.data));

            return;
        }

        dispatch(setIsLoadingCityData(true));
        fetch(baseURL + '/rest/cities?_' + Date.now())
            .then(res => res.json())
            .then(cityData => {
                dispatch(setCityData(cityData));

                localStorage.setItem(
                    'city_data_cache',
                    JSON.stringify({
                        expires: Date.now() + 60 * 1000,
                        data: cityData,
                    })
                );

                dispatch(setIsLoadingCityData(false));
            })
            .catch(() => {
                dispatch(setIsLoadingCityData(false));
            });
    };
}
